package com.sskj.wallet.presenter;

import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.wallet.bean.GasPriceBean;
import com.sskj.wallet.bean.TransferResultBean;
import com.sskj.wallet.http.HttpConfig;
import com.sskj.wallet.ui.activity.TransferActivity;


public class TransferActivityPresenter extends BasePresenter<TransferActivity> {

    public void transfer(String memo,String fromPri, String toAddr, String cttAddr, String amount, String gasLimit, String gasPriceGwei) {
        if (cttAddr == null) {
            httpService.transferEth(memo,fromPri, toAddr, amount, gasLimit, gasPriceGwei)
                    .execute(new JsonCallBack<HttpData<TransferResultBean>>(this) {
                        @Override
                        public void onSuccess(Response<HttpData<TransferResultBean>> response) {
                            HttpData<TransferResultBean> httpData = response.body();
                            if (httpData.getStatus() == HttpConfig.OK) {
                                if (httpData.getData().getFlag() == 0) {
                                    mView.transferSuccess();
                                } else {
                                    ToastUtil.showShort(httpData.getData().getErr());
                                }
                            }
                        }
                    });
        } else {
            httpService.transferEthToken(memo,fromPri, toAddr, cttAddr, amount, gasLimit, gasPriceGwei)
                    .execute(new JsonCallBack<HttpData<TransferResultBean>>(this) {
                        @Override
                        public void onSuccess(Response<HttpData<TransferResultBean>> response) {
                            HttpData<TransferResultBean> httpData = response.body();
                            if (httpData.getStatus() == HttpConfig.OK) {
                                if (httpData.getData().getFlag() == 0) {
                                    mView.transferSuccess();
                                } else {
                                    ToastUtil.showShort(httpData.getData().getErr());
                                }
                            }
                        }
                    });

        }
    }

    public void getGasPrice() {
        httpService.getGasPrice()
                .execute(new JsonCallBack<HttpData<GasPriceBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<GasPriceBean>> response) {
                        HttpData<GasPriceBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.gasPrice(httpData.getData());

                        }
                    }
                });
    }
}
